package jp.co.apl.haven_enquete.android.activity;

import jp.co.apl.haven_enquete.android.R;
import jp.dev7.android.util.Activities;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

public class ActivityCommon {

    private static final int MENU_CATEGORY_LIST = 0;
    private static final int MENU_UPLOAD = 1;
    private static final int MENU_BASIC_INFO_EDIT = 2;
    private static final int MENU_CLOSE = 3;

    private Activity activity;

    public ActivityCommon(Activity activity) {
        super();
        this.activity = activity;
    }

    /**
     * メニューボタンクリック時.
     * 
     * @param menu
     *            -
     * @return -
     */
    public boolean onCreateOptionsMenu(Menu menu) {

        int order = 0;
        if (activity instanceof CategoryList == false) {
            menu.add(0, MENU_CATEGORY_LIST, order++, "カテゴリ一覧").setIcon(
                android.R.drawable.ic_menu_slideshow);
        }
        if (activity instanceof Upload == false) {
            menu.add(0, MENU_UPLOAD, order++, "回答送信").setIcon(
                android.R.drawable.ic_menu_upload);
        }
        if (activity instanceof BasicInfoInput == false) {
            menu.add(0, MENU_BASIC_INFO_EDIT, order++, "基本情報編集").setIcon(
                android.R.drawable.ic_menu_edit);
        }
        menu.add(0, MENU_CLOSE, order++, "終了").setIcon(
            android.R.drawable.ic_menu_close_clear_cancel);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_CATEGORY_LIST: {

            // カテゴリ一覧へ遷移
            Intent intent = new Intent(activity, CategoryList.class);
            activity.startActivity(intent);

            break;
        }
        case MENU_UPLOAD: {

            // 回答送信画面へ遷移
            Intent intent = new Intent(activity, Upload.class);
            activity.startActivity(intent);

            break;
        }
        case MENU_BASIC_INFO_EDIT: {

            // 基本情報入力画面へ遷移
            Intent intent = new Intent(activity, BasicInfoInput.class);
            activity.startActivity(intent);

            break;
        }
        case MENU_CLOSE: {

            Activities.finishApplication(activity);

            break;
        }
        default:
            break;
        }
        return true;
    }

    public void setActionBar(String title) {
        activity.getWindow().setFeatureInt(
            Window.FEATURE_CUSTOM_TITLE,
            R.layout.action_bar);

        Activities.setText(activity, R.id.actionBar_title, title);

        // ヘッダ部ボタンへのイベントハンドラセット
        Activities.setClickListnerToButton(
            activity,
            R.id.actionBar_prevButton,
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.onBackPressed();
                }
            });
    }

}
